package com.test.deencredit.dialog_deencredit;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentActivity;

import com.test.deencredit.R;
import com.test.deencredit.bean_deencredit.config_deencredit.AppShowCustomize_deencredit;
import com.test.deencredit.bean_deencredit.userinfo_deencredit.BankAccount_Fr_deencredit;
import com.test.deencredit.util_deencredit.http_deencredit.HttpUtil_deencredit;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;


public class DeleteAccountDialog_Fr_deencredit extends Dialog {

    private final FragmentActivity activity_deencredit;

    private final BankAccount_Fr_deencredit account_deencredit;

    private final String page_deencredit;

    private final Runnable dltActRespRunnable_deencredit;

    public DeleteAccountDialog_Fr_deencredit(@NonNull FragmentActivity activity, BankAccount_Fr_deencredit account, String page, Runnable dltActRespRunnable) {
        super(activity);
        this.activity_deencredit = activity;
        this.account_deencredit = account;
        this.page_deencredit = page;
        this.dltActRespRunnable_deencredit = dltActRespRunnable;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_delete_account_fr_deencredit);

        getWindow().setBackgroundDrawableResource(R.color.transparent);
        setCanceledOnTouchOutside(false);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

        initView_deencredit();
    }

    @SuppressLint("SetTextI18n")
    private void initView_deencredit(){

        TextView accountmobileTv  = findViewById(R.id.ddaf_tv_accountmobile);
        ImageView accountlogoIv = findViewById(R.id.ddaf_iv_logo);
        TextView accountchannelTv = findViewById(R.id.ddaf_tv_accountchannel);

        //取消
        findViewById(R.id.ddaf_btn_cancel).setOnClickListener(v -> {
            HttpUtil_deencredit.getInstance_deencredit().saveBuriedPoint_deencredit(page_deencredit,"_DELETE_CANCEL_CLICK", account_deencredit.getWalletMobile_deencredit());
            dismiss();
        });

        //提交
        findViewById(R.id.ddaf_btn_submit).setOnClickListener(v -> {
            HttpUtil_deencredit.getInstance_deencredit().saveBuriedPoint_deencredit(page_deencredit,"_DELETE_CONFIRM_CLICK", account_deencredit.getWalletMobile_deencredit());
            HttpUtil_deencredit.getInstance_deencredit().deleteAccount_Fr_deencredit(activity_deencredit, account_deencredit.getId_deencredit(),() -> {
                if (dltActRespRunnable_deencredit !=null) dltActRespRunnable_deencredit.run();
            });
            dismiss();
        });

        if (account_deencredit != null){

            String channelDesc = account_deencredit.getWalletDesc_deencredit();

            accountmobileTv.setText(account_deencredit.getWalletMobile_deencredit());
            accountchannelTv.setText(channelDesc);

            //加载图标
            if (AppShowCustomize_deencredit.getInstance_deencredit().getChannelLogoByDesc_deencredit(channelDesc) == null){
                new Thread(() -> {
                    try {
                        if (TextUtils.isEmpty(account_deencredit.getWalletLogo_deencredit())) return;
                        if (!AppShowCustomize_deencredit.getInstance_deencredit().hasAccountChannel_deencredit()) return;
                        URL url = new URL(account_deencredit.getWalletLogo_deencredit());
                        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                        connection.setConnectTimeout(5000);
                        connection.setRequestMethod("GET");
                        if (connection.getResponseCode()==200){
                            InputStream inputStream = connection.getInputStream();
                            Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                            AppShowCustomize_deencredit.getInstance_deencredit().setChannelLogoByDesc_deencredit(channelDesc,bitmap);
                            new Handler(Looper.getMainLooper()).post(() -> accountlogoIv.setImageBitmap(bitmap));
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }).start();
            }else accountlogoIv.setImageBitmap(AppShowCustomize_deencredit.getInstance_deencredit().getChannelLogoByDesc_deencredit(channelDesc));

        }

    }

}
